package com.xb.controller;

import com.github.pagehelper.PageInfo;
import com.xb.entity.Dict;
import com.xb.entity.Drug;
import com.xb.service.IDictService;
import com.xb.service.IDrugService;
import com.xb.util.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Controller
 * /api/drug
 */
@RestController
@RequestMapping("/api/drug")
public class DrugController {
    @Autowired
    private IDrugService drugService;
    @Autowired
    private IDictService dictService;

    /**
     * 条件查询
     */
    @GetMapping("/getDrug")
    @RequiresPermissions("system:drug:find")
    public Object find() {
        Result result = new Result();
        List<Drug> drugs = drugService.selectAllDrugList();
        result.setData(drugs);
        return result;
    }

    /**
     * 条件+分页查询
     * 如果是layui分页查询，pageNum 替换成 page  ， pageSize 替换成 limit
     * 如果是bootstrap分页，则page 替换成 pageNum， limit 替换成 pageSize
     */
    @RequestMapping("/page")
    @RequiresPermissions("system:drug:page")
    public Object findPage(Drug obj, @RequestParam(value = "page", defaultValue = "2") Integer page,
                           @RequestParam(value = "limit", defaultValue = "5") Integer limit) throws UnsupportedEncodingException {
        Result result = new Result();
        PageInfo<Drug> pageInfo = drugService.selectDrugList(obj, page, limit);
        if (pageInfo != null) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setData(pageInfo.getList());
            result.setTotal(pageInfo.getTotal());
        } else {
            result.setStatus(Result.RESPONSE_FAIL);
        }
        return result;
    }

    /**
     * 修改
     */
    @PutMapping
    @RequiresPermissions("system:drug:update")
    public Object update(@RequestBody Drug obj) {
        Result result = new Result();
        if (drugService.updateDrug(obj) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("编辑成功");
        } else {
            result.setStatus(Result.RESPONSE_FAIL);
            result.setMessage("编辑失败");
        }
        return result;
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("system:drug:del")
    public Object del(@PathVariable Integer id) {
        Result result = new Result();
        if (drugService.deleteDrugById(id) > 0) {
            result.setStatus(Result.RESPONSE_SUCCESS);
            result.setMessage("删除成功");
        } else {
            result.setStatus(Result.RESPONSE_FAIL);
            result.setMessage("删除失败");
        }
        return result;
    }

    // *****************************************************************
    // ****************以上是自己使用laiui写的表格***************************
    // *****************************************************************
    // *****************************************************************
    // *****************************************************************
    @PostMapping("/getAllDrugType")
    public List<String> getAllDrugType(String typeNo) {
        return dictService.selectDictByTypeNo(typeNo);
    }

    @PostMapping("/getAllEfficacyClassification")
    public List<String> getAllEfficacyClassification(String typeNo) {
        return dictService.selectDictByTypeNo(typeNo);
    }

    @PostMapping("/getAllDrugName")
    public List<String> getAllDrugName(String typeNo) {
        return dictService.selectDictByTypeNo(typeNo);
    }

    @GetMapping("/getAllDrug")
    public PageInfo<Drug> getAllDrug(Drug obj, @RequestParam(value = "pageNumber", defaultValue = "1") Integer page,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer limit) {
        return drugService.selectDrugList(obj, page, limit);
    }

    @PostMapping("/updateDrug")
    public Result updateDrug(@RequestBody Drug drug) {
        return drugService.updateDrugAndDict(drug);
    }

    @PostMapping("/deleteDrug")
    public Result deleteDrug(String name, Integer id) {
        return drugService.deleteDrugAndDict(name, id);
    }

    @PostMapping("/getDrugInfo")
    public Drug getDrugInfo(String name) {
        return drugService.getDrugInfo(name);
    }

    @PostMapping("/addStorageQuantity")
    public Result addStorageQuantity(Drug drug){
        return drugService.addStorageQuantity(drug);
    }

    @PostMapping("/addNewDrug")
    public Result addNewDrug(@RequestBody Drug drug){
        return drugService.addNewDrug(drug);
    }

    @PostMapping("/addDrugType")
    public Result addDrugType(Dict dict){
        return drugService.addDrugType(dict);
    }
}
